﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EMR
{
    public partial class frm_EditPatient : Form
    {
        private string _Message;
        private string _BedNo, _Name, _AdmissionNo;
        private string Gender;
        private string _BeforeAdNo;
        private string _BeforeBedNo;

        //private IPatientBll _patientBll;
        private Patient patient
        {
            get;
            set;
        }
        public frm_EditPatient(string message)
        {
            _Message = message;
            InitializeComponent();
            SolveMessage();
            this.patient = new Patient();

            //string sql = "SELECT  Name FROM tb_Department ";
            //var sqlDataReader = (SqlDataReader)SqlHelper.ExecuteQuery(sql);
            var sqlDataReader = DepartmentDal.SelectAll();
            while (sqlDataReader.Read())
            {
                this.cbx_Department.Items.Add(sqlDataReader[0]);
            };

            load();
            Gender = this.rdb_Female.Checked == true ? "女" : "男";
            this._BeforeAdNo = txb_AdmissionNo.Text;
            this._BeforeBedNo = txb_BedNo.Text;
        }

        private void btn_Submit_Click(object sender, EventArgs e)
        {

            if (txb_AdmissionNo.Text == "" || txb_Name.Text == "" || txb_VisitingStaff.Text == "" || cbx_Department.Text == ""
              || txb_BedNo.Text == "" || dtp_EnterTime.Text == "" || this.Gender == "")
            {
                MessageBox.Show("提示", "红色标签为必填，请确认是否填写完整");

            }
            else
            {

                DateTime dt =
                    Convert.ToDateTime(this.dtp_EnterTime.Value.ToString("yyyy-MM -dd HH:mm:ss"));
                this.patient.AdmissionNo = txb_AdmissionNo.Text.Trim();
                this.patient.BedNo = txb_BedNo.Text.Trim();
                this.patient.EnterTime = dt;
                this.patient.Name = txb_Name.Text.Trim();
                this.patient.VisitingStaff = txb_VisitingStaff.Text.Trim();
                this.patient.Gender = this.Gender;
                this.patient.Department = cbx_Department.Text.Trim();

                if (PatientBll.UpdatePatient(patient,this._BeforeAdNo,this._BeforeBedNo))                                                                       //若成功写入1行记录；
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }

            }

        }

        private void btn_Abort_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rdb_Male_CheckedChanged(object sender, EventArgs e)
        {
            this.Gender = (sender as RadioButton).Text;
        }

        private void rdb_Female_CheckedChanged(object sender, EventArgs e)
        {
            this.Gender = (sender as RadioButton).Text;
        }

        private void cbx_Department_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbx_Department.Text != "")
            {
                this.cbx_InpatientWard.Text = this.cbx_Department.Text + "病区";
            }
            else
            {
                this.cbx_InpatientWard.Text = "";
            }
        }

        private void SolveMessage()
        {
            string[] B = new string[3];
            int a, b, c;
            for (int i = 0; i < 3; i++)
            {
                a = _Message.IndexOf(":");
                b = _Message.IndexOf("\n");
                c = _Message.Length;
                if (b < a - 1)
                {
                    break;
                }
                B[i] = _Message.Substring(a + 1, b - a - 1);//获取床号,姓名，住院号；
                _Message = _Message.Substring(b + 1, c - b - 1);
            }
            //foreach (string C in B)
            //{
            //    MessageBox.Show(C);
            //}
            _BedNo = B[0];
            _Name = B[1];
            _AdmissionNo = B[2];
        }
        private void load()
        {
            // string sql = 
            //     "SELECT AdmissionNo,Name,Gender,BedNo,VisitingStaff,EnterTime,Department  " +
            //     "FROM tb_Patient  WHERE BedNo=@BedNo AND Name=@Name AND AdmissionNo=@AdmissionNo;";
            // SqlParameter[] pms = new SqlParameter[]
            // {
            //     new SqlParameter("@BedNo", _BedNo),
            //     new SqlParameter("@Name", _Name),
            //     new SqlParameter("@AdmissionNo", _AdmissionNo),
            //};
            //   var dr = (SqlDataReader)SqlHelper.ExecuteQuery(sql, pms);

            var dr = PatientBll.load(_BedNo, _Name, _AdmissionNo);
            int                                                                                             //声明整型变量，并调用数据阅读器的方法GetOrdinal获取各列相应的索引值；
                noOrdinal = dr.GetOrdinal("AdmissionNo")
                , nameOrdinal = dr.GetOrdinal("Name")
                , genderOrdinal = dr.GetOrdinal("Gender")
                , bedNoOrdinal = dr.GetOrdinal("BedNo")
                , visitingstaffOrdinal = dr.GetOrdinal("VisitingStaff")
                , enterTimeOrdinal = dr.GetOrdinal("EnterTime")
                , departmentOrdinal = dr.GetOrdinal("Department");
            if (dr.Read())                                                                       //若数据阅读器成功读取到下一条记录（首次查询则表示第一条记录）；
            {
                this.txb_AdmissionNo.Text = dr.GetString(noOrdinal);                                         //通过各列的索引值来访问列，从而避免每次都通过列名称来定位列；
                this.txb_Name.Text = dr.GetString(nameOrdinal);                                                 //通过强类型Get方法获取指定类型的数据，从而避免装箱/拆箱；
                this.rdb_Female.Checked = dr.GetString(genderOrdinal)  == "女";
                this.rdb_Male.Checked = dr.GetString(genderOrdinal)  == "男";
                this.txb_BedNo.Text = dr.GetString(bedNoOrdinal);
                this.txb_VisitingStaff.Text = dr.GetString(visitingstaffOrdinal);
                this.dtp_EnterTime.Value = dr.GetDateTime(enterTimeOrdinal);
             //   this.txb_BirthDate.Text = sqlDataReader.GetDateTime(birthDateOrdinal).ToShortDateString();
                this.cbx_Department.Text = dr.GetString(departmentOrdinal);
                //this.txb_Speciality.Text =                                                                  //学生特长可为空值，而强类型Get方法无法读取数据库空值，需借助方法IsDBNull判断并处理空值；
                //    sqlDataReader.IsDBNull(specialityOrdinal) ? null : sqlDataReader.GetString(specialityOrdinal);
            }
            //sqlDataReader.Close();;

        }
    }
}
